/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.core;

import java.util.ArrayList;
import java.util.List;

import org.apache.solr.client.solrj.SolrQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.solrdimension.config.DimensionConfigWrapper;
import com.google.code.solrdimension.parsers.exception.SolrDimensionException;
import com.google.code.solrdimension.querymodifiers.QueryModifier;

public class DimensionQuery {
    
    private SolrQuery solrQuery;
    
    private QueryModifier queryModifier;
    
    private DimensionConfigWrapper solrDimensionConfig;
    
    private static Logger logger = LoggerFactory.getLogger(DimensionQuery.class);
    
    /**
     * must be a list of String in the format of "SolrfieldName:filtervalue"
     */
    private List<String> filtersToApply;
    
    
    
    
    public DimensionQuery(SolrQuery query, DimensionConfigWrapper solrDimensionconfig, QueryModifier queryModifier) throws SolrDimensionException{
        
        if (query == null || queryModifier == null || solrDimensionconfig == null){
            throw new SolrDimensionException("One or more of the required parameters is / are null.");
        }
        
        this.queryModifier = queryModifier;
        this.solrQuery = query;
        this.solrDimensionConfig = solrDimensionconfig;
        
        
        
    }
    
    public void setFiltersToApply(List<String> filtersToApply){
        
        this.filtersToApply = filtersToApply;
        
    }
    
    public void addFilterToApply(String filterToApply){
        
        if (filtersToApply == null){
            filtersToApply = new ArrayList<String>();
        }
        
        filtersToApply.add(filterToApply);
        
    }
    

    public SolrQuery getSolrQuery(){
        
        try {
            queryModifier.prepareQuery(solrQuery, solrDimensionConfig);
        } catch (SolrDimensionException e) {
            logger.error("Problem preparing the query...");
            e.printStackTrace();
        }
        
        return solrQuery;
    }

    

}
